import {_decorator, Animation, Component, lerp, Node, Vec3} from 'cc';
import {AudioManager} from "db://assets/scripts/common/AudioManager";
import {BaseInfo} from "db://assets/scripts/data/model/BaseInfo";
import {GameManager} from "db://assets/scripts/game/GameManager";
import {PoolManager} from "db://assets/scripts/common/PoolManager";
import {EffectManager} from "db://assets/scripts/common/EffectManager";
import {Skill} from "db://assets/scripts/data/model/Skill";
import {AudioPaths} from "db://assets/scripts/common/AudioPaths";
import {MonsterController} from "db://assets/scripts/game/role/monster/MonsterController";

const {ccclass, property} = _decorator;

/**
 * 台风S型脚本
 */
@ccclass('TornadoEffect')
export class TornadoEffect extends Component {
    /**
     * 移动动画
     */
    @property(Animation)
    public moveAnimation: Animation;

    /**
     * 技能信息
     */
    public skill: Skill;
    /**
     * 基础信息
     */
    public baseInfo: BaseInfo;

    /**
     * 当前坐标和玩家坐标的向量差
     */
    private offsetPosition: Vec3 = new Vec3();
    /**
     * 初始位置
     */
    private originWorldPosition: Vec3 = new Vec3();
    /**
     * 目标速度
     */
    private targetSpeed: number = 0;
    /**
     * 当前速度
     */
    private currentSpeed: number = 0;
    /**
     * 目标位置
     */
    private targetWorldPosition: Vec3 = new Vec3();
    /**
     * 当前位置
     */
    private currentWorldPosition: Vec3 = new Vec3();
    /**
     * 箭节点超过玩家这个范围则隐藏
     */
    private disappearRange: number = 25;

    /**
     * 初始化
     *
     * @param skill 技能信息
     * @param baseInfo 基础信息
     * @param parentScript 使用者
     */
    public init(skill: Skill, baseInfo: BaseInfo, parentScript?: MonsterController) {
        this.skill = skill;
        this.baseInfo = baseInfo;
        parentScript?.warningScript?.hideWarning();
        this.originWorldPosition.set(this.node.worldPosition);
        this.currentWorldPosition.set(this.node.worldPosition);
        Vec3.subtract(this.offsetPosition, this.currentWorldPosition, GameManager.getInstance().gamePlayerProxy.getPlayerNode().worldPosition);
        this.offsetPosition.normalize().negative();
        Vec3.add(this.currentWorldPosition, this.currentWorldPosition, this.offsetPosition.multiplyScalar(2));
        this.node.setWorldPosition(this.currentWorldPosition);

        EffectManager.getInstance().playParticle(this.node);
        this.targetSpeed = skill.speed;
        this.currentSpeed = this.targetSpeed * 0.5;

        this.moveAnimation.getState("tornado").time = 0;
        this.moveAnimation.getState("tornado").sample();
        this.moveAnimation.play();

        AudioManager.getInstance().playSound(AudioPaths.tornadoSound);

    }

    update(deltaTime: number) {
        if (GameManager.getInstance().gamePlayerProxy.getPlayerNode()) {
            //朝forward方向飞行
            this.currentSpeed = lerp(this.targetSpeed, this.currentSpeed, 0.25);
            this.targetWorldPosition.set(0, 0, -deltaTime * this.currentSpeed);
            this.node.translate(this.targetWorldPosition, Node.NodeSpace.LOCAL);
            this.currentWorldPosition.set(this.node.worldPosition);

            //超过玩家一定范围则隐藏
            Vec3.subtract(this.offsetPosition, this.currentWorldPosition, GameManager.getInstance().gamePlayerProxy.getPlayerNode().worldPosition);
            if (this.offsetPosition && this.offsetPosition.length() >= this.disappearRange) {
                PoolManager.getInstance().putNode(this.node);
            }
        }
    }
}